﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LibrarySystem.Models;
using LibrarySystem.LibraryUtils.Entities;
using LibrarySystem.LibraryUtils.DAL.DAOs;

namespace LibrarySystem.Controllers.LibraryControllers
{
    [Authorize]
    public class BookController : Controller
    {
        [HttpPost]
        public JsonResult BookOptions(string bookName = null)
        {
            try
            {
                Library_Books libBook = new Library_Books();
                List<BookModel> bookModel = libBook.GetDataList();
                var bookOptions = bookModel.Select(s => new { Value = s.ID, DisplayText = s.BookName });

                return Json(new { Result = "OK", Options = bookOptions });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }

        [HttpPost]
        public JsonResult GetBookList(string bookName = "", string bookAuthor = "", int category = 0, int jtStartIndex = 0, int jtPageSize = 0, string jtSorting = null)
        {
            try
            {
                Library_Books libBook = new Library_Books();
                int count = libBook.GetDataCount(null, bookName, bookAuthor, category);
                List<BookModel> bookList = libBook.GetDataList(null, bookName, bookAuthor, category, jtStartIndex, jtPageSize, jtSorting);
                return Json(new { Result = "OK", Records = bookList, TotalRecordCount = count });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }

        [HttpPost]
        public JsonResult AddBook(BookModel model)
        {
            try
            {
                Library_Books libBook = new Library_Books();
                if (!ModelState.IsValid)
                {
                    return Json(new { Result = "Error", Message = "The data is invalid. Please try again!" });
                }
                var add = libBook.AddNewData(model);
                return Json(new { Result = "OK", Record = add });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "Error", Message = ex.Message });
            }
        }

        [HttpPost]
        public JsonResult UpdateBook(BookModel model)
        {
            try
            {
                Library_Books libBook = new Library_Books();
                if (!ModelState.IsValid)
                {
                    return Json(new { Result = "Error", Message = "The data is invalid. Please try again!" });
                }
                libBook.UpdateData(model);
                return Json(new { Result = "OK" });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "Error", Message = ex.Message });
            }
        }

        [HttpPost]
        public JsonResult DeleteBook(int Id)
        {
            return null;
        }

    }
}
